//===== Custom eAthena Script =======================================
//= SQL Guild Bank
//===== By: =========================================================
//= Unstoppable
//= Edited by Windsor
//===== Description =================================================
//= A Guild Bank where the Guild Master has control:
//= Everyone can deposit money but not everyone can withdraw
//= It also has a logging system where the GM can see who deposited/
//= withdrew how much and when.
//===== Version Change Log ==========================================
//= 1.0 - Initial Release
//===================================================================
function	script	GuildBank	{
	set .@gid, getcharid(2);
	set .@npcname$, "[Banker]";

	mes .@npcname$;
	setarray .@DynamicMenu$[0],"Show the current balance","Deposit zeny","Withdraw zeny";
	mes "What would you like to do?";
	next;
	if( strcharinfo(0) == getguildmaster(.@gid) )
		setarray .@DynamicMenu$[3],"Register a Guild Bank account", "Allow someone to withdraw", "Remove an allowed", "Show allowed ones", "Deposit Logs", "Withdrawal Logs";

	for( set .@i,0; .@i<getarraysize(.@DynamicMenu$); set .@i,.@i+1 )
	{
		set .@menu$, .@menu$ + .@DynamicMenu$[.@i]+":";
		set .@select$[.@j], .@DynamicMenu$[.@i];
		set .@j, .@j+1;
	}
	set .@menu, select(.@menu$)-1;

	if( .@select$[.@menu] == "Show the current balance" )
	{
		callfunc "CheckIfHasBankAcc",.@npcname$,.@gid;
		mes .@npcname$;
		query_sql "SELECT `amount` FROM `guild_bank` WHERE `guildid`="+.@gid,.@amount$;	//returns the amount of zeny in the bank
		mes "There is currently ^0000FF" + callfunc("F_intComa",.@amount$) + " zeny^000000 in the Guild Bank";
		close2;
		cutin "", 255;
		end;
	}

	if( .@select$[.@menu] == "Deposit zeny" )
	{
		callfunc "CheckIfHasBankAcc",.@npcname$,.@gid;
		mes .@npcname$;
		mes "How much zeny would you like to deposit?";
		next;
		input .@input;
		if( .@input < 1 )
		{
			close2;
			cutin "", 255;
			end;
		}
		if( .@input > Zeny )
		{
			mes .@npcname$;
			mes "You do not have enough zeny!";
			close2;
			cutin "", 255;
			end;
		}
		set Zeny, Zeny-.@input;
		query_sql "UPDATE `guild_bank` SET `amount` = `amount` +"+.@input+" WHERE `guildid`="+.@gid,.@esc$;
		query_sql "INSERT INTO `guild_banklog` VALUES ("+.@gid+",'"+strcharinfo(0)+"',now() ,'DEPOSIT',"+.@input+")",.@esc$;
		mes .@npcname$;
		mes "Thank you";
		close2;
		cutin "", 255;
		end;
	}

	if( .@select$[.@menu] == "Withdraw zeny" )
	{
		callfunc "CheckIfHasBankAcc",.@npcname$,.@gid;
		set .@name$, strcharinfo(0);
		query_sql "SELECT `allowed` FROM `guild_bankallowees` WHERE `guildid`="+.@gid+" AND `charname` = '"+.@name$+"'",.@allowed$;
		if( .@allowed$ == "TRUE" )
		{
			mes .@npcname$;
			mes "How much would you like to withdraw?";
			next;
			input .@input;
			if( .@input < 1 )
			{
				close2;
				cutin "", 255;
				end;
			}
			query_sql "SELECT `amount` FROM `guild_bank` WHERE `guildid`="+.@gid,.@amount;
			if( .@input > .@amount )
			{
				mes .@npcname$;
				mes "You can't withdraw more zeny than your Guildbank currently holds";
				close2;
				cutin "", 255;
				end;
			} else {
				query_sql "UPDATE `guild_bank` SET `amount` = `amount` -"+.@input+" WHERE `guildid`="+.@gid,.@esc$;
				query_sql "INSERT INTO `guild_banklog` VALUES ("+.@gid+",'"+strcharinfo(0)+"',now() ,'WITHDRAWAL',"+.@input+")",.@esc$;
				set Zeny, Zeny + .@input;
				close2;
				cutin "", 255;
				end;
			}
		} else {
			mes .@npcname$;
			mes "You're not allowed to withdraw zeny from the Guild Bank";
			close2;
			cutin "", 255;
			end;
		}
	}

	if( .@select$[.@menu] == "Register a Guild Bank account" )
	{
		query_sql "SELECT `guildid` FROM `guild_bank` WHERE `guildid`="+.@gid,.@exists;
		if( .@gid == .@exists )
		{
			mes .@npcname$;
			mes "This guild already has a bank account registered.";
		} else {
			mes .@npcname$;
			mes "You're guild has registered a Guild Bank.";
			query_sql "INSERT INTO `guild_bank` VALUES ("+.@gid+",0)",.@esc$;
		}
		close2;
		cutin "", 255;
		end;
	}

	if( .@select$[.@menu] == "Allow someone to withdraw" )
	{
		callfunc "CheckIfHasBankAcc",.@npcname$,.@gid;
		mes "[" + .@npcname$ + "]";
		mes "Who do you allow to withdraw zeny?";
		mes "Make sure you spell the name right.";
		next;
		input .@name$;
		query_sql "SELECT `name` FROM `char` WHERE `name`= '"+escape_sql(.@name$)+"'",.@exists$;					//checks if char exists
		if( .@name$ != .@exists$ )
		{
			mes "[" + .@npcname$ + "]";
			mes .@name$+" is not a valid name, the person doesn't exist";
			close2;
			cutin "", 255;
			end;
		}
		query_sql "SELECT `allowed` FROM `guild_bankallowees` WHERE `guildid`="+.@gid+" AND `charname` = '"+escape_sql(.@name$)+"'",.@allowed$;
		if( .@allowed$ == "TRUE" )
		{
			mes "[" + .@npcname$ + "]";
			mes "This character is already allowed.";
			close2;
			cutin "", 255;
			end;
		}
		mes "[" + .@npcname$ + "]";
		mes .@name$+" is now allowed to withdraw on the Guild Bank.";
		query_sql "REPLACE INTO `guild_bankallowees` VALUES ("+.@gid+",'"+escape_sql(.@name$)+"','TRUE')",.@esc$;	//allow player into withdrawing
		close2;
		cutin "", 255;	
		end;
	}

	if( .@select$[.@menu] == "Remove an allowed" )
	{
		callfunc "CheckIfHasBankAcc",.@npcname$,.@gid;
		mes "[" + .@npcname$ + "]";
		mes "Who do you want to block from withdrawing zeny?";
		next;
		query_sql "SELECT `charname` FROM `guild_bankallowees` WHERE `guildid`= '"+.@gid+"' AND `allowed` = 'TRUE' ORDER BY `charname`",.@exists$;
		mes .@npcname$;
		mes "Choose a player from the list. This player will no longer be allowed to withdraw zeny from the Guild Bank";
		next;
		for( set .@i,0; .@i<getarraysize(.@exists$); set .@i,.@i+1 )
		{
			set .@menu$, .@menu$ + .@exists$[.@i] +":";
			set .@select$[.@c], .@exists$[.@i];
			set .@c, .@c +1;
		}
		set .@menu, select(.@menu$) -1;

		mes "[" + .@npcname$ + "]";
		mes .@select$[.@menu]+" has been blocked from withrawing zeny";
		query_sql "UPDATE `guild_bankallowees` SET `allowed` = 'FALSE' WHERE `charname` = '"+.@select$[.@menu]+"'",.@esc$;
		close2;
		cutin "", 255;
		end;
	}

	if( .@select$[.@menu] == "Show allowed ones" )
	{
		callfunc "CheckIfHasBankAcc",.@npcname$,.@gid;
		query_sql "SELECT `charname` FROM `guild_bankallowees` WHERE `guildid`= '"+.@gid+"' ORDER BY `charname`",.@exists$;
		set .@index, getarraysize(.@exists$);
		for( set .@i,0; .@i<.@index; set .@i,.@i+1)
			mes .@exists$[.@i];
		close2;
		cutin "", 255;
		end;
	}

	if( .@select$[.@menu] == "Deposit Logs" )
	{
		callfunc "CheckIfHasBankAcc",.@npcname$,.@gid;
		query_sql "SELECT `charname` FROM `guild_banklog` WHERE `guildid`= '"+.@gid+"' AND `action` = 'DEPOSIT' ORDER BY `time` DESC",.@dNames$;
		query_sql "SELECT `time` FROM `guild_banklog` WHERE `guildid`= '"+.@gid+"' AND `action` = 'DEPOSIT' ORDER BY `time` DESC",.@dTimes$;
		query_sql "SELECT `amount` FROM `guild_banklog` WHERE `guildid`= '"+.@gid+"' AND `action` = 'DEPOSIT' ORDER BY `time` DESC",.@dAmounts$;
		mes "[" + .@npcname$ + "]";
		mes "All people that have deposited money in the Guild Bank will be shown from recent to old and 1 per page.";
		next;
		for( set .@i,0; .@i<getarraysize(.@dNames$); set .@i,.@i+1)
		{
			mes "[" + .@npcname$ + "]";
			mes "^000000Name: ^FF0000" + .@dNames$[.@i];
			mes "^000000Date/Time: ^FF0000" + .@dTimes$[.@i];
			mes "^000000Amount: ^FF0000" + .@dAmounts$[.@i] + "z^000000";
			mes " ";
			mes " ";
			mes (.@i+1)+"/"+getarraysize(.@dNames$);
			if( .@i < (getarraysize(.@dNames$) -1) ) next;
		}
		close2;
		cutin "", 255;
		end;
	}

	if( .@select$[.@menu] == "Withdrawal Logs" )
	{
		callfunc "CheckIfHasBankAcc",.@npcname$,.@gid;
		query_sql "SELECT `charname` FROM `guild_banklog` WHERE `guildid`= '"+.@gid+"' AND `action` = 'WITHDRAWAL' ORDER BY `time` DESC",.@wNames$;
		query_sql "SELECT `time` FROM `guild_banklog` WHERE `guildid`= '"+.@gid+"' AND `action` = 'WITHDRAWAL' ORDER BY `time` DESC",.@wTimes$;
		query_sql "SELECT `amount` FROM `guild_banklog` WHERE `guildid`= '"+.@gid+"' AND `action` = 'WITHDRAWAL' ORDER BY `time` DESC",.@wAmounts$;
		mes "[" + .@npcname$ + "]";
		mes "All people that have withdrawn money from the Guild Bank will be shown from recent to old and 1 per page.";
		next;
		for( set .@i,0; .@i<getarraysize(.@wNames$); set .@i,.@i+1)
		{
			mes "[" + .@npcname$ + "]";
			mes "^000000Name: ^FF0000" + .@wNames$[.@i];
			mes "^000000Date/Time: ^FF0000" + .@wTimes$[.@i];
			mes "^000000Amount: ^FF0000" + .@wAmounts$[.@i] + "z^000000";
			mes " ";
			mes " ";
			mes (.@i+1)+"/"+getarraysize(.@wNames$);
			if( .@i < (getarraysize(.@wNames$) -1) ) next;
		}
		close2;
		cutin "", 255;
		end;
	}
	close2;
	cutin "", 255;
	end;
}

function	script	CheckIfHasBankAcc	{
	query_sql "SELECT `guildid` FROM `guild_bank` WHERE `guildid`= '"+getarg(1)+"'",.@HasBankAcc;
	if( !.@HasBankAcc )
	{
		mes "[Banker]";
		mes "Your Guild doesn't have a Bank Account, only the Guild Master can register one.";
		close2;
		cutin "", 255;
		end;
	}
	return;
}